package cn.wubo.multi.thread.demo03;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * Spring Async
 */
@Service
public class Demo0301 {

    @Async("defaultThreadPoolExecutor")
    public void doThing(String name) {
        //等待一会
        try {
            Thread.sleep((int) (Math.random() * 100));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " " + name);
    }

    @Async("defaultThreadPoolExecutor")
    public Future<String> doThingThenReturn(String name) {
        //等待一会
        try {
            Thread.sleep((int) (Math.random() * 100));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " " + name);
        return new AsyncResult<>(name);
    }
}
